package com.qingyun.modular.distributor.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qingyun.common.persistence.dao.UserMapper;
import com.qingyun.modular.distributor.dao.DistributorDao;
import com.qingyun.modular.distributor.service.IDistributorService;
import com.qingyun.modular.distributor.vo.DistributorVo;
import com.qingyun.modular.model.Distributor;
import com.qingyun.modular.order.dao.OrdersDao;
import com.qingyun.modular.order.vo.OrdersVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单Dao
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DistributorServiceImpl extends ServiceImpl<DistributorDao, Distributor> implements IDistributorService {

    @Resource
    OrdersDao ordersDao;


    @Resource
    DistributorDao distributorDao;
    /**
     * 分页获取订单列表
     * @param id 订单id
     * @param userId 分销商id
     * @param beginTime 订单开始时间
     * @param endTime 订单结束时间
     * @return
     */
    @Override
    public List<OrdersVo>  selectList(String id, String userId, String beginTime, String endTime) {
        List<OrdersVo> resluts = new ArrayList<>();

        List<OrdersVo> list = ordersDao.selectList(id, userId, beginTime, endTime);
        for (int i=0;i<list.size();i++) {
            OrdersVo ordersVo = list.get(i);
            //封裝
            OrdersVo vo = new OrdersVo();
            vo.setId(ordersVo.getId());
            vo.setUserId(ordersVo.getUserId());
            vo.setOrderNo(ordersVo.getOrderNo());

            //处理订单状态
            String orderStatusdb = ordersVo.getOrderStatus();
            String orderStatus="未知";//默认值
            if("1".equals(orderStatusdb)){
                orderStatus="交易成功";
            }else if("2".equals(orderStatusdb)){
                orderStatus="已取消";
            }
            vo.setOrderStatus(orderStatus);


            //处理订单类型
            String orderTypedb = ordersVo.getOrderType();
            String orderType="未知";//默认值
            if("1".equals(orderTypedb)){
                orderType="商品订单";
            }else if("2".equals(orderTypedb)){
                orderType="其他订单";
            }
            vo.setOrderType(orderType);

            vo.setPrice(ordersVo.getPrice());

            //处理支付方式
            String payTypedb = ordersVo.getPayType();
            String payType="未知";//默认值
            if("1".equals(payTypedb)){
                payType="微信支付";
            }else if("2".equals(payTypedb)){
                payType="支付宝支付";
            }else if("3".equals(payTypedb)){
                payType="小程序支付";
            }
            vo.setPayType(payType);

            vo.setGoodsID(ordersVo.getGoodsID());
            vo.setOpenid(ordersVo.getOpenid());
            vo.setTransactionId(ordersVo.getTransactionId());

            //处理是否支付
            String isPaydb = ordersVo.getIsPay();
            String isPay="未知";//默认值
            if("1".equals(isPaydb)){
                isPay="已支付";
            }else if("2".equals(isPaydb)){
                isPay="未支付";
            }
            vo.setIsPay(isPay);
            vo.setPaySuccAt(ordersVo.getPaySuccAt());
            vo.setCreatedAt(ordersVo.getCreatedAt());
            vo.setUpdateAt(ordersVo.getUpdateAt());

            //处理结算类型
            String settlementStatusdb = ordersVo.getSettlementStatus();
            String settlementStatus="未知";//默认值
            if("1".equals(settlementStatusdb)){
                settlementStatus="已结算";
            }else if("2".equals(settlementStatusdb)){
                settlementStatus="未结算";
            }else if("3".equals(settlementStatusdb)){
                settlementStatus="结算失败";
            }
            vo.setSettlementStatus(settlementStatus);
            vo.setSettlementAt(ordersVo.getSettlementAt());

            vo.setCover(ordersVo.getCover());
            vo.setGoodsName(ordersVo.getGoodsName());
            vo.setShopName(ordersVo.getShopName());

            resluts.add(vo);
        }

        return resluts;
    }

    //导出
    @Override
    public List<OrdersVo> selectListExport(String id, String userId, String beginTime, String endTime) {
        List<OrdersVo> resluts = new ArrayList<>();

        List<OrdersVo> list = ordersDao.selectListExport(id, userId, beginTime, endTime);
        for (int i=0;i<list.size();i++) {
            OrdersVo ordersVo = list.get(i);
            //封裝
            OrdersVo vo = new OrdersVo();
            vo.setId(ordersVo.getId());
            vo.setUserId(ordersVo.getUserId());
            vo.setOrderNo(ordersVo.getOrderNo());

            //处理订单状态
            String orderStatusdb = ordersVo.getOrderStatus();
            String orderStatus="未知";//默认值
            if("1".equals(orderStatusdb)){
                orderStatus="交易成功";
            }else if("2".equals(orderStatusdb)){
                orderStatus="已取消";
            }
            vo.setOrderStatus(orderStatus);


            //处理订单类型
            String orderTypedb = ordersVo.getOrderType();
            String orderType="未知";//默认值
            if("1".equals(orderTypedb)){
                orderType="商品订单";
            }else if("2".equals(orderTypedb)){
                orderType="其他订单";
            }
            vo.setOrderType(orderType);

            vo.setPrice(ordersVo.getPrice());

            //处理支付方式
            String payTypedb = ordersVo.getPayType();
            String payType="未知";//默认值
            if("1".equals(payTypedb)){
                payType="微信支付";
            }else if("2".equals(payTypedb)){
                payType="支付宝支付";
            }else if("3".equals(payTypedb)){
                payType="小程序支付";
            }
            vo.setPayType(payType);

            vo.setGoodsID(ordersVo.getGoodsID());
            vo.setOpenid(ordersVo.getOpenid());
            vo.setTransactionId(ordersVo.getTransactionId());

            //处理是否支付
            String isPaydb = ordersVo.getIsPay();
            String isPay="未知";//默认值
            if("1".equals(isPaydb)){
                isPay="已支付";
            }else if("2".equals(isPaydb)){
                isPay="未支付";
            }
            vo.setIsPay(isPay);
            vo.setPaySuccAt(ordersVo.getPaySuccAt());
            vo.setCreatedAt(ordersVo.getCreatedAt());
            vo.setUpdateAt(ordersVo.getUpdateAt());

            //处理结算类型
            String settlementStatusdb = ordersVo.getSettlementStatus();
            String settlementStatus="未知";//默认值
            if("1".equals(settlementStatusdb)){
                settlementStatus="已结算";
            }else if("2".equals(settlementStatusdb)){
                settlementStatus="未结算";
            }else if("3".equals(settlementStatusdb)){
                settlementStatus="结算失败";
            }
            vo.setSettlementStatus(settlementStatus);
            vo.setSettlementAt(ordersVo.getSettlementAt());

            vo.setGoodsName(ordersVo.getGoodsName());
            vo.setShopName(ordersVo.getShopName());

            resluts.add(vo);
        }

        return resluts;
    }

    @Override
    public List<DistributorVo> selectDistributorList(int userId) {
        List<DistributorVo> distributorVos1 = distributorDao.selectDistributorList(userId);
        for (DistributorVo distributor1:
                distributorVos1) {
            List<DistributorVo> distributorVos2 = distributorDao.selectDistributorList(distributor1.getDistributorId());
        }
        return null;
    }

}
